using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Plugin.Sns.Scene;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
namespace MicroMsg.UI.UserContrl
{
	public class SnsHttpImgCtrlBase : UserControl
	{
		private static int sNID = 0;
		public int mNID;
		private bool mBIsLoaded;
		public static DependencyProperty ItemObjectProperty = DependencyProperty.Register("ObjMediaInfo", typeof(TloMedia), typeof(SnsHttpImgCtrlBase), new PropertyMetadata(null, new PropertyChangedCallback(SnsHttpImgCtrlBase.OnItemObjectPropertyChanged)));
		private snsMediaDownloadInfo mRequestingImgInfo;
		private static List<EventConst> sListEventId = new List<EventConst>();
		private static Dictionary<string, List<SnsHttpImgCtrlBase>> sDictWaitCtrls = new Dictionary<string, List<SnsHttpImgCtrlBase>>();
		private static EventWatcher sWatcher;
		private static object sObjectWatcher = new object();
		private object mObject;
		public event MMUIEvent mDlgObjectGetted;
		public TloMedia ObjMediaInfo
		{
			get
			{
				return (TloMedia)base.GetValue(SnsHttpImgCtrlBase.ItemObjectProperty);
			}
			set
			{
				base.SetValue(SnsHttpImgCtrlBase.ItemObjectProperty, value);
			}
		}
		public SnsHttpImgCtrlBase()
		{
			this.mNID = SnsHttpImgCtrlBase.sNID++;
			base.add_Loaded(new RoutedEventHandler(this.AsyncLoadCtrlBase_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.AsyncLoadCtrlBase_Unloaded));
			ShowObjectInMemory.AddOneObjectRecord(this);
		}
		~SnsHttpImgCtrlBase()
		{
			ShowObjectInMemory.RemoveOneObjectRecord(this);
		}
		protected virtual void Ctrl_Loaded()
		{
		}
		protected virtual void Ctrl_UnLoaded()
		{
		}
		private void AsyncLoadCtrlBase_Unloaded(object sender, RoutedEventArgs e)
		{
			this.mBIsLoaded = false;
			this.Ctrl_UnLoaded();
		}
		private void AsyncLoadCtrlBase_Loaded(object sender, RoutedEventArgs e)
		{
			this.mBIsLoaded = true;
			this.Ctrl_Loaded();
			this.objectGetted(this.mObject);
			this.mObject = null;
		}
		private static void OnItemObjectPropertyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_Property() == SnsHttpImgCtrlBase.ItemObjectProperty)
			{
				(dependencyObject_0 as SnsHttpImgCtrlBase).ItemObjectPropertyChanged(dependencyPropertyChangedEventArgs_0);
			}
		}
		private void ItemObjectPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_OldValue() != dependencyPropertyChangedEventArgs_0.get_NewValue())
			{
				if (dependencyPropertyChangedEventArgs_0.get_OldValue() != null && dependencyPropertyChangedEventArgs_0.get_NewValue() != null)
				{
					TloMedia tloMedia = dependencyPropertyChangedEventArgs_0.get_OldValue() as TloMedia;
					TloMedia tloMedia2 = dependencyPropertyChangedEventArgs_0.get_NewValue() as TloMedia;
					if (tloMedia != null && tloMedia2 != null && tloMedia.strUrl == tloMedia2.strUrl && tloMedia.strThumb == tloMedia2.strThumb)
					{
						return;
					}
				}
				this.MediaInfoChanged(dependencyPropertyChangedEventArgs_0.get_NewValue() as TloMedia);
			}
		}
		public void StartRequest(object objReqInfo, bool bIsSyncLoad = true)
		{
			if (this.mRequestingImgInfo != null)
			{
				this.StopRequest();
			}
			if (SnsHttpImgCtrlBase.sWatcher == null)
			{
				SnsHttpImgCtrlBase.registerEvent();
			}
			if (objReqInfo == null)
			{
				this.GetObjectFailed(null);
				return;
			}
			if (objReqInfo is snsMediaDownloadInfo)
			{
				snsMediaDownloadInfo snsMediaDownloadInfo = objReqInfo as snsMediaDownloadInfo;
				if (snsMediaDownloadInfo.string_0 != null)
				{
					SnsMediaMgr.getImg(snsMediaDownloadInfo, bIsSyncLoad);
					if (snsMediaDownloadInfo.object_0 != null && snsMediaDownloadInfo.object_0 is BitmapImage)
					{
						this.StopRequest();
						this.objectGetted(snsMediaDownloadInfo);
						return;
					}
					SnsHttpImgCtrlBase.AddToWaitList(snsMediaDownloadInfo.string_0, this);
					return;
				}
				else
				{
					this.GetObjectFailed(null);
				}
			}
		}
		public void StopRequest()
		{
			if (this.mRequestingImgInfo != null)
			{
				if (this.mRequestingImgInfo.string_0 != null)
				{
					SnsHttpImgCtrlBase.RemoveFromWaitList(this.mRequestingImgInfo.string_0, this);
				}
				this.mRequestingImgInfo = null;
			}
		}
		private static void AddToWaitList(string strUrl, SnsHttpImgCtrlBase item)
		{
			if (string.IsNullOrEmpty(strUrl))
			{
				return;
			}
			List<SnsHttpImgCtrlBase> list;
			if (SnsHttpImgCtrlBase.sDictWaitCtrls.ContainsKey(strUrl))
			{
				list = SnsHttpImgCtrlBase.sDictWaitCtrls.get_Item(strUrl);
			}
			else
			{
				list = new List<SnsHttpImgCtrlBase>();
				SnsHttpImgCtrlBase.sDictWaitCtrls.set_Item(strUrl, list);
			}
			if (!list.Contains(item))
			{
				list.Add(item);
			}
		}
		private static void RemoveFromWaitList(string strUrl, SnsHttpImgCtrlBase item)
		{
			if (string.IsNullOrEmpty(strUrl))
			{
				return;
			}
			List<SnsHttpImgCtrlBase> list = null;
			if (SnsHttpImgCtrlBase.sDictWaitCtrls.ContainsKey(strUrl))
			{
				list = SnsHttpImgCtrlBase.sDictWaitCtrls.get_Item(strUrl);
			}
			if (item != null && list.Contains(item))
			{
				list.Remove(item);
				if (list.get_Count() == 0)
				{
					SnsHttpImgCtrlBase.sDictWaitCtrls.Remove(strUrl);
				}
			}
		}
		private static void NotifyCome(string strUrl, object object_0)
		{
			if (string.IsNullOrEmpty(strUrl))
			{
				return;
			}
			List<SnsHttpImgCtrlBase> list = null;
			if (SnsHttpImgCtrlBase.sDictWaitCtrls.ContainsKey(strUrl))
			{
				list = SnsHttpImgCtrlBase.sDictWaitCtrls.get_Item(strUrl);
			}
			if (list != null)
			{
				using (List<SnsHttpImgCtrlBase>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						SnsHttpImgCtrlBase current = enumerator.get_Current();
						if (current != null)
						{
							current.ObjectCome(object_0);
						}
					}
				}
				SnsHttpImgCtrlBase.sDictWaitCtrls.Remove(strUrl);
			}
		}
		protected virtual void MediaInfoChanged(TloMedia mediaInfo)
		{
			if (mediaInfo == null)
			{
				this.StopRequest();
			}
		}
		protected virtual void ObjectCome(object object_0)
		{
			if (this.mDlgObjectGetted != null)
			{
				if (object_0 == null || !(object_0 is snsMediaDownloadInfo))
				{
					return;
				}
				snsMediaDownloadInfo snsMediaDownloadInfo = object_0 as snsMediaDownloadInfo;
				if (snsMediaDownloadInfo.object_0 == null)
				{
					return;
				}
				this.mDlgObjectGetted(this, snsMediaDownloadInfo.object_0 as BitmapImage);
			}
		}
		protected virtual void GetObjectFailed(object object_0)
		{
		}
		protected virtual bool IsRightObject(object object_0)
		{
			return true;
		}
		private static void registerEvent()
		{
			if (SnsHttpImgCtrlBase.sWatcher == null)
			{
				SnsHttpImgCtrlBase.sWatcher = new EventWatcher(SnsHttpImgCtrlBase.sObjectWatcher, null, new EventHandlerDelegate(SnsHttpImgCtrlBase.OnEvent));
				EventCenter.registerEventWatcher(EventConst.ON_MGR_SNS_GET_MEDIA_FINISH, SnsHttpImgCtrlBase.sWatcher);
				EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_SNS_POST_ERR, SnsHttpImgCtrlBase.sWatcher);
			}
		}
		private static void unRegisterEvent()
		{
			if (SnsHttpImgCtrlBase.sWatcher != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_MGR_SNS_GET_MEDIA_FINISH, SnsHttpImgCtrlBase.sWatcher);
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_SNS_POST_ERR, SnsHttpImgCtrlBase.sWatcher);
				SnsHttpImgCtrlBase.sWatcher = null;
			}
		}
		private void objectGetted(object object_0)
		{
			if (object_0 == null)
			{
				return;
			}
			this.StopRequest();
			this.ObjectCome(object_0);
		}
		private static void getOneSnsImg(snsMediaDownloadInfo info)
		{
			if (info == null)
			{
				return;
			}
			if (info.string_0 != null)
			{
				SnsHttpImgCtrlBase.NotifyCome(info.string_0, info);
			}
		}
		private static void OnEvent(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			SnsPageMgr.setTimeStamp("app show image finish");
			if (evtArgs.mEventID == EventConst.ON_MGR_SNS_GET_MEDIA_FINISH)
			{
				if (evtArgs.mObject == null)
				{
					return;
				}
				if (evtArgs.mObject is List<snsMediaDownloadInfo>)
				{
					List<snsMediaDownloadInfo> listObject = evtArgs.getListObject<snsMediaDownloadInfo>();
					if (listObject == null)
					{
						return;
					}
					using (List<snsMediaDownloadInfo>.Enumerator enumerator = listObject.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							snsMediaDownloadInfo current = enumerator.get_Current();
							SnsHttpImgCtrlBase.getOneSnsImg(current);
						}
						return;
					}
				}
				SnsHttpImgCtrlBase.getOneSnsImg(evtArgs.mObject as snsMediaDownloadInfo);
			}
		}
		public void SetUrl(string strUrl)
		{
			snsMediaDownloadInfo objReqInfo = new snsMediaDownloadInfo
			{
				snsObjID = 0uL,
				string_0 = strUrl,
				urlType = SnsUrlType.MMSNS_URL_CDN,
				priority = SnsPriority.LOW
			};
			this.StartRequest(objReqInfo, true);
		}
	}
}
